import dva from 'dva';
import { browserHistory, hashHistory } from 'dva/router';
import createLoading from 'dva-loading';
import { Toast } from 'antd-mobile';
import _ from 'lodash';
import * as wxService from './services/wxService';
import styles from './index.css';
import "lib-flexible"
const ERROR_MSG_DURATION = 1;

// 1. Initialize
const app = dva({
  history: hashHistory,
  onError(e) {
    // Toast.fail(e.message, ERROR_MSG_DURATION);
  },
  onEffect(effect, { put }, model, actionType) {
    return function* (...args) {
      if (!_.get(args, '[0].quiet', false)) {
        Toast.loading('加载中...', 0);
      }
      yield effect(...args);
      if (!_.get(args, '[0].quiet', false)) {
        Toast.hide();
      }
    };
  },
});


// 2. Plugins
// app.use({});
app.use(createLoading());

// 3. Model
app.model(require('./models/auth'));
app.model(require('./models/task'));
app.model(require('./models/home'));
app.model(require('./models/stat'));
app.model(require('./models/vip'));
app.model(require('./models/exam'));
app.model(require('./models/association'));
app.model(require('./models/coupon'));
app.model(require('./models/knowledge'));
app.model(require('./models/help'));
app.model(require('./models/bonus'));
app.model(require('./models/appointment'));
app.model(require('./models/im'));
app.model(require('./models/customizeTask'));

// 4. Router
app.router(require('./router'));

// 5. Start
app.start('#root');

const url = location.href.split('#')[0];
wxService.wxJsConfig(url).then((config) => {
  if (typeof wx !== 'undefined') {
    let cc = config.data;
    cc.jsApiList.push('openLocation', 'getLocation');
    console.log("wx config:", cc);
    wx.config && wx.config(cc);
  }
});

window.addEventListener('hashchange', function (e) {
  if(e.newURL.indexOf('/#/?')!==-1&&e.oldURL.indexOf('/#/?')!==-1){
    return
  }
  document.querySelector('body').classList.add(styles.animate);
})
document.body.addEventListener('animationend', function () {
  document.querySelector('body').classList.remove(styles.animate);
})

window.app = app;
